package com.shd.edanpei.photo;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import com.shd.edanpei.R;
import com.shd.edanpei.util.ToastUtil;

import java.util.ArrayList;


/**
 * Created by mc on 2018/12/7.
 * yang-wen-wen
 */

public class PhotoViewActivity extends AppCompatActivity {

    public static final String PHOTO_LIST = "photo_list";
    public static final String CURRENT_POSITION = "current_position";

    private ViewPager mViewPager;

    private PhotoViewAdapter mAdapter;
    private PhotoManager photoManager;

    private ArrayList<String> mPhotoList;
    private Bitmap mBitmap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_view);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        mViewPager = findViewById(R.id.view_pager);
    }

    private void initData() {
        mPhotoList = getIntent().getStringArrayListExtra(PHOTO_LIST);

        mAdapter = new PhotoViewAdapter(this, mPhotoList);
        mViewPager.setAdapter(mAdapter);

        photoManager = new PhotoManager(this);
    }

    private void initListener() {
        if (mPhotoList.size() <= 1) {

        } else {
            mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    ToastUtil.showToast(position + "");
                }
            });
        }
        mAdapter.setOnClickListener(new PhotoViewAdapter.OnClickListener() {

            @Override
            public void onClick() {
                finish();
            }

            @Override
            public void onLongClick(final Bitmap bitmap) {
                AlertDialog.Builder builder = new AlertDialog.Builder(PhotoViewActivity.this);
                builder.setItems(new String[]{"保存图片"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        photoManager.savePhoto(bitmap);
                    }
                });
                builder.show();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
